package com.hdax.controller;

import com.hdax.entity.*;
import com.hdax.mappers.WagesMapper;
import com.hdax.service.*;
import com.hdax.utils.ResultUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.*;

@Controller
@EnableScheduling
@RequestMapping("/wages")
public class WagesController {

    @Autowired
    /**
     * 工资
     */
    private WagesService wagesService;
    @Autowired
    /**
     * 部门
     */
    private DepartmentService departmentService;

    @Autowired
    /**
     * 员工
     */
    private EmployeeService employeeSer;

    @Autowired
    /**
     * 角色
     */
    private RoleService roleService;

    @Autowired
    /**
     * 考勤
     */
    private AttendanceService attendanceService;

    @Autowired
    /**
     * 状态
     */
    private StateService stateService;

    @Autowired
    private EmployeeInfoService employeeInfoService;

    /**
     * 加载,查询所有时间
     * @param model
     * @return
     */
    @GetMapping("/index")
    public String list(Model model) {
        model.addAttribute("depar",wagesService.CountMoney());
        return "wages/WagesProfiles";
    }
     /**
     *根据名称/月份模糊查询
     * @param page
     * @param limit
     * @param wages
     * @return
     */
    @PostMapping(path = "/list")
    @ResponseBody
    public Map<String,Object> list(@RequestParam("page")int page, @RequestParam("limit")int limit, @Param("wages") Wages wages,int id){
        Map<String,Object> map = new HashMap<String, Object>();
        //获取当前登录的人的id,在部门表中查询是否是项目经理
        List<Department> dlist=departmentService.listAll(id);
        map.put("code", 0);
        map.put("msg", "");
        if(id==1){
            if (wages.getEmployeeid()!=null || wages.getMoneydate()!=null){
                List<Wages> wagesList=wagesService.findAll((page-1)*limit,limit,wages);
                map.put("count", wagesList.size());
                map.put("data", wagesList);
            }else{
                if (page-1!=0 || limit!=0){
                    List<Wages> wagesList=wagesService.findAll((page-1)*limit,limit,wages);
                    map.put("count",wagesService.Count());
                    map.put("data", wagesList);
                }else if (dlist.size()==0){
                    wages.setEmployeeid(id);
                    List<Wages> wagesList=wagesService.findAll((page-1)*limit,limit,wages);

                    //查询总条数(该名称下)
                    int count= wagesService.CountByname(wages);
                    map.put("count",count);
                    //集合
                    map.put("data", wagesList);
                }else{
                    List<Wages> konglist=new ArrayList<>();
                    //总条数
                    int count=0;
                    for (int i = 0; i < dlist.size(); i++) {
                        //获取部门编号
                        int did=dlist.get(i).getId();
                        List<EmployeeInfo> elist=employeeInfoService.emplist(did);
                        for (int j = 0; j < elist.size(); j++) {
                            //获取员工的编号
                            int empid=elist.get(j).getId();
                            List<Wages> wages1=wagesService.findByeid(empid);
                            for (int k = 0; k < wages1.size(); k++) {
                                //获取工资表id
                                int wid=wages1.get(k).getId();
                                Wages att=wagesService.selectById(wid);
                                konglist.add(att);
                                count++;
                            }
                        }
                    }
                    //查询总条数(该名称下)
                    map.put("count", count);
                    map.put("data", konglist);
                }
            }
        }else{
            wages.setEmployeeid(id);
            EmployeeInfo employeeInfo= employeeInfoService.findEmployeeInfoById(id);
            wages.setStaffname(employeeInfo.getEmpName());
           List<Wages> wagesList= wagesService.findAll((page-1)*limit,limit,wages);
            map.put("count", wagesList.size());
            map.put("data", wagesList);
        }
        return map;
    }


    //启动定时任务
    //每隔一个月计算一次
    @Scheduled(cron = "* * * * 1 *")
    public void dingshi(){
        insert();
    }


    @PostMapping(path = "/insert")
    @ResponseBody
    @Transactional(rollbackFor=Exception.class)
    public ResultUtil insert(){
        //获取本月时间
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM");
        Date date = new Date(System.currentTimeMillis());
        //当前的年月
        String sjdate=formatter.format(date);

        //查询当月的所有工资数据
        List<Wages> listw=wagesService.insertDate(sjdate);
        for (Wages wages:listw) {
            int eid=wages.getId();
            wagesService.deleByid(eid);
        }
        int i =0;
        List<EmployeeInfo> list=employeeInfoService.selectAll();
        for (EmployeeInfo employeeInfo:list) {
            //当前员工编号
            int eid=employeeInfo.getId();
            //当前员工部门
            int did=employeeInfo.getDepartment().getId();
            Wages wages=new Wages();
            wages.setEmployeeid(eid);
            wages.setDepartment(did);
            i = wagesService.insertById(wages);
        }
        if(i > 0){
            return ResultUtil.upDataResult("计算成功！");
        }else {
            return ResultUtil.upDataResult("计算失败！");
        }

    }


}
